<?php
// vim:set expandtab tabstop=3 shiftwidth=3 softtabstop=3 foldclose=all foldmethod=marker:
/**
 * 项目：   5anet
 * 文件：   class/album/ShowIndex.class.php
 *
 * 显示相册首页
 *
 * PHP Version 5
 *
 * @package:   class.album
 * @author:    Mike.G Chinese Name: 黄叶 <hylinux@gmail.com>
 * @license:   http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1 
 * @copyright: www.5anet.com
 * @version:   $Id: $
 * @date:      $Date: $
 */

include_once CLASS_PATH.'main/BaseAction.class.php';

//包含相册的工具表
include_once CLASS_PATH.'album/AlbumUtil.class.php';



class ShowIndex extends BaseAction {
   /**
    * 数据库的连接
    */
   private $db;

   /**
    * 每页显示的照片数 
    */
   private $pre_page = 24;


   /**
    * 构造函数
    * @param:  NULL
    * @return: NULL
    * @access: public
    */
   public function __construct() {
      $this->db = $this->getDB();
   }


   /**
    * 显示相册首页
    * @param:  NULL
    * @return: NULL
    * @access: public
    */
   public function run() {
      //取得分类的id
      $id = $this->getParameterFromGET('id');

      if ( !$id ) {
         $id = 0;
      }

      if ( !AlbumUtil::isExists($this->db, $id)  ) {
         $id = 0;
      }

      
      $smarty = $this->getSmarty();

      //取得用户当前查看的目录
      $page = $this->getParameterFromGET('page');
      
      if ( !$page || $page < 0  ) {
         $page = 1;
      }

      $total_page = AlbumUtil::getTotalPageByType($this->db, $id, $this->pre_page);
      
      if ( $page > $total_page && $total_page > 0 ) {
         $page = $total_page;
      } else if ( $total_page == 0 ) {
         $page = 0;
      }

      //页面
      $smarty->assign('now_page', $page);
      $smarty->assign('total_page', $total_page);

      $begin_page = 1;
      $end_page = $total_page;

      if ( $page <= 10 && $total_page >=10  ) {
         $end_page = 10; 
      } else if ( $page > 10  ) {
         if ( $page % 10 == 0 ) {
            //向前翻
            $end_page = $page; 
            $begin_page = $end_page - 9;
         } else if ( $page % 10 == 1 ) {
            //向后翻
            //确定开始的页数
            $begin_page = $page; 
            if ( $begin_page > $total_page ) {
               $begin_page = $page - 9;
            }
            if ( ( $begin_page + 9 ) > $total_page ) {
               $end_page = $total_page;
            } else {
               $end_page = $begin_page + 9;
            } 
   
         } else {
            $num = $page % 10;
            $pre_num = floor($page / 10 );
            $begin_page = $pre_num * 10 + 1;
            $end_page = $begin_page + 9;
         }
      }

      if ( $end_page > $total_page ) {
         $end_page = $total_page;
      }

      $nav_page_array = array();
      for( $i = $begin_page; $i<=$end_page; $i++ ) {
         array_push($nav_page_array, $i);
      }

      //帖子导航栏
      $smarty->assign('nav_page', $nav_page_array);

      $smarty->assign('category', $id);


      $offset_page = ( $page - 1 ) * $this->pre_page;





      
      //取得所有的相册分类
      $category = AlbumUtil::getAllCategory($this->db);
      $smarty->assign('all_category', $category);

      $album_info = AlbumUtil::getAlbumInfo($this->db, $id, 20, $offset_page);
      $smarty->assign('album', $album_info);

      //为SEO
      $smarty->assign('clone_title', '5anet相册首页  5anet.com' );
      $smarty->assign('header_meta_content', '5anet相册首页  5anet.com' );


      //取得页面



      

      $smarty->display('albumindex.tmpl');


   }

}

?>
